package com.etime.shycourse.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.dto.CourseAndCommentDto;
import com.etime.shycourse.dto.CoursecommentDto;
import com.etime.shycourse.pojo.Coursecomment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author wb
 * @since 2022-03-02
 */
@Repository
public interface CoursecommentMapper extends BaseMapper<Coursecomment> {
    /**
     * 统计评论条数
     * 根据课程id
     *
     * @param CourseId
     * @return Int
     */
    Integer countComment(Integer CourseId);

    /**
     * 我的提问
     * <p>
     * 根据用户的id 查询 我的提问
     * 列表
     *
     * @param userId
     * @return List<Coursecomment> 评论表
     **/
    List<CoursecommentDto> selectMyQuestionsCommentList(Integer userId);


    /**
     * 我的评论
     * <p>
     * 根据用户的id 查询我的评论
     * 列表
     *
     * @param userId
     * @return List<Coursecomment> 评论表
     **/
    List<CoursecommentDto> selectMyCommentList(Integer userId);

    /**
     * 评论，提问后台管理模块
     * <p>
     * 根据课程id 查询 评论和提问
     *
     * @param CourseID
     * @return List<Coursecomment>
     **/
    List<CoursecommentDto> selectSysCommentList(Integer CourseID);

    /**
     * 根据评论id查询一条评论的详细信息。
     * 会查询出评论的课程名称
     *
     * @param commentid 评论id
     * @return
     */
    CoursecommentDto selectCommentById(Integer commentid);

/**
 */
    /**
     * 课程评论管理 大类的
     * 后台管理模块 分页
     * <p>
     * 查询评论内容 课程信息
     * 条件 根据课程 id ，模糊搜索课程名 ， 评论类型，
     * 创建时间降序 未回复 是否置顶 是否加精
     * <p>
     * 根据课程id 查询 评论和提问
     *
     * @param coursecomment
     * @return Page<Coursecomment>
     **/
    Page<CoursecommentDto> selectSysCommentPages(CoursecommentDto coursecomment, Page<CoursecommentDto> page);


    /**
     * 根据课程id查询课程的评论，分页查询，并查询出评论的回复次数
     *
     * @param courseid
     * @param page
     * @return
     */
    @Select("select a.*,b.recommentcount from coursecomment a left outer join (" +
            "select commentpid,count(commentid) as recommentcount from coursecomment " +
            "where commentpid<>-1 " +
            "group by commentpid) b on a.commentid=b.commentpid " +
            "where a.courseid=#{courseid}  and a.commentpid=-1 and commentstate=1 " +
            "and commentisshow=1 order by commenttop desc,commentdigest desc,commenttime desc")
    Page<CoursecommentDto> selectCommnetPageByCourseId(@Param("courseid") Integer courseid, @Param("page") Page<CoursecommentDto> page);

    /**
     * 根据用户的id查询用户对课程的评论或者提问。按时间顺序倒序排列
     *
     * @param userid      用户id
     * @param commenttype 提问/评论
     * @param page        分页参数
     * @return
     */
    @Select("select a.*,b.recommentcount,c.coursetitle from coursecomment a left outer join ( " +
            "select commentpid,count(commentid) as recommentcount from coursecomment " +
            "where commentpid<>-1 " +
            "group by commentpid) b on a.commentid=b.commentpid " +
            "inner join course c on a.courseid=c.courseid " +
            "where a.commentuser=#{userid} and a.commenttype=#{commenttype}  and a.commentpid=-1 " +
            " order by commentre asc,commenttime desc")
    Page<CoursecommentDto> selectCommentPageByUserId(@Param("userid") Integer userid,
                                                     @Param("commenttype") Integer commenttype,
                                                     @Param("page") Page<CoursecommentDto> page);

    /**
     * 根据教师id查询出教师所有课程的评论和提问
     * 排序规则：教师未回复的排在最前面，其次使用时间降序排列
     *
     * @param teacherid   教师id
     * @param commenttype 提问/评论
     * @param page        分页参数
     * @return
     */
    @Select("select a.*,b.recommentcount,c.coursetitle from coursecomment a left outer join ( " +
            "select commentpid,count(commentid) as recommentcount from coursecomment " +
            "where commentpid<>-1 " +
            "group by commentpid) b on a.commentid=b.commentpid " +
            "inner join course c on a.courseid=c.courseid and c.courseteacherid=#{teacherid} " +
            "where  a.commentpid=-1 and a.commenttype=#{commenttype} " +
            "order by commentre asc,commenttime desc")
    Page<CoursecommentDto> selectCommentPageByTeacherId(@Param("teacherid") Integer teacherid,
                                                        @Param("commenttype") Integer commenttype,
                                                        @Param("page") Page<CoursecommentDto> page);

    /**
     * 根据教师id查询教师还有多少未回复的提问
     *
     * @param teacherid 教师id
     * @return
     */
    @Select("select count(commentid) from course a " +
            "inner join coursecomment b on a.courseid=b.courseid " +
            "where a.courseteacherid=#{teacherid} and b.commenttype=2 and b.commentre=0")
    int selectNotReCommentByTeacherId(@Param("teacherid") Integer teacherid);

    /**
     * 后台 管理课程 /----修改
     * 统计评论的条数 ，数据
     *
     * @param CourseId 课程id
     * @return CourseAndCommentDto
     */
    CourseAndCommentDto selectCourseAndCommentDtoByIds(Integer CourseId);
}
